/**
 * Created by MD-3 on 2017/6/8.
 */

function getDeviceChart() {
    var deviceChartData = getDeviceChartData();
    var legendData = deviceChartData.deviceNames;
    var seriesData = deviceChartData.seriesDatas;

    var option_pie = {
        title : {
            text: '中继-终端设备工作状态统计',
            x:'center'
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: legendData
        },
        series : [
            {
                name: '正常工作的终端数量',
                type: 'pie',
                radius : '45%',
                center: ['45%', '45%'],
                data: seriesData,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    getChart(option_pie);
}

function getDeviceChartData() {
    var deviceMarker;
    var deviceNames = [];
    var seriesDatas = [];
    var deviceName = '';
    var workStateCount = 0;
    for(var i in deviceMarkerList){
        deviceMarker = deviceMarkerList[i];
        if(deviceMarker.type != 0){ continue;}                      //筛选中继
        deviceNames.push(deviceMarker.name);
        deviceName = deviceMarker.name;
        for(var j in deviceMarkerList){
            deviceMarker = deviceMarkerList[j];
            if(deviceMarker.type != 1){ continue;}                  //筛选终端
            if(deviceMarker.workState=='0'){
                workStateCount++;
            }
        }
        seriesDatas.push({value:workStateCount, name:deviceName})
    }
    return {"deviceNames":deviceNames, "seriesDatas":seriesDatas};
}


function getAlarmChart() {
    var alarmChartData = getAlarmChartData();
    var legendData = alarmChartData.deviceNames;
    var seriesData = alarmChartData.seriesDatas;

    var option_pie = {
        title : {
            text: '中继-终端设备报警统计',
            x:'center'
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: legendData
        },
        series : [
            {
                name: '报警的终端数量',
                type: 'pie',
                radius : '45%',
                center: ['45%', '45%'],
                data: seriesData,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    getChart(option_pie);
}


function getAlarmChartData() {
    var deviceMarker;
    var deviceNames = [];
    var seriesDatas = [];
    var deviceName = '';
    var alarmStateCount = 0;
    for(var i in deviceMarkerList){
        deviceMarker = deviceMarkerList[i];
        if(deviceMarker.type != 0){ continue;}                      //筛选中继
        deviceNames.push(deviceMarker.name);
        deviceName = deviceMarker.name;
        for(var j in deviceMarkerList){
            deviceMarker = deviceMarkerList[j];
            if(deviceMarker.type != 1){ continue;}                  //筛选终端
            if(deviceMarker.alarmState!='0'){
                alarmStateCount++;
            }
        }
        seriesDatas.push({value:alarmStateCount, name:deviceName})
    }
    return {"deviceNames":deviceNames, "seriesDatas":seriesDatas};
}


function getChart(option_pie) {
    var dialogchart = document.getElementById('dialog-chart');
    dialogchart.style.display = "block";

    //移动弹框
    jQuery(document).ready(function() {
        $('div.dialog-title').mousedown(
            function(event) {
                var isMove = true;
                var dialogChart = $('#dialog-chart');
                var abs_left = parseInt(dialogChart.css('marginLeft'));
                var abs_top = parseInt(dialogChart.css('marginTop'));

                var abs_x = event.pageX - dialogChart.offset().left;
                var abs_y = event.pageY - dialogChart.offset().top;

                $(document).mousemove(function(event) {
                    if (isMove) {
                        var obj = $('#dialog-chart');
                        obj.css({
                            'left': event.pageX - abs_x - abs_left,
                            'top': event.pageY - abs_y - abs_top
                        });
                    }
                }).mouseup(
                    function() {
                        isMove = false;
                    }
                );
            }
        );
    });

    var myChart_pie = echarts.init(document.getElementById('chart-pie'));

    myChart_pie.setOption(option_pie);
}

function closeChart() {
    var dialogchart = document.getElementById('dialog-chart');
    dialogchart.style.display = "none";
}







